﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ProjectCloth.Entity;

namespace ProjectCloth.FrmUI.ColorCards
{
    public partial class ColorCardFrm : UserControl
    {
        private ClothContext db = new ClothContext();
        public ColorCardFrm()
        {
            InitializeComponent();
        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(cardNoTxt.Text) || string.IsNullOrEmpty(cardQuantityTxt.Text))
                {
                    MessageBox.Show("编号或者数量不能为空");
                    return;
                }
                var time = createTimePick.Value;
                var cardNo = cardNoTxt.Text;
                var cardQty = Convert.ToDecimal(cardQuantityTxt.Text);
                //var carColorQty = string.IsNullOrEmpty(cardColorQtyTxt.Text) ? 0 : Convert.ToDecimal(cardColorQtyTxt.Text);
                if (cardQty <= 0)// || carColorQty < 0)
                {
                    MessageBox.Show("数量要大于0！");
                    return;
                }
                var card = new ColorCard()
                {
                    ColorCardNo = cardNo,
                    ColorQuantity = 0,// carColorQty,
                    Quantity = cardQty,
                    CreateTime = DateTime.Now,
                    MakeTime = time
                };
                db.ColorCards.Add(card);
                if (db.SaveChanges() > 0)
                {
                    MessageBox.Show("保存成功！");

                }
                else
                {
                    MessageBox.Show("保存失败！");
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("出现错误，" + error.ToString());
                var log = error.ToString();
            }
            //var time = createTimePick.Value;
            cardNoTxt.Text = "";
            cardQuantityTxt.Text = "";
            //cardColorQtyTxt.Text = "";
            createTimePick.Focus();
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            base.ProcessCmdKey(ref msg, keyData);
            switch (keyData)
            {
                case Keys.Enter:
                    if (cardQuantityTxt.Focused == true)
                    {
                        saveBtn_Click(null, null);
                        break;
                    }
                    else
                    {
                        SendKeys.Send("{Tab}"); break;
                    }

            }
            return false;
        }

        private void cardNoTxt_Enter(object sender, EventArgs e)
        {
            var form = new ProductSelectFrm();
            var result = form.ShowDialog();
            if (!string.IsNullOrEmpty(form.productNo))
                cardNoTxt.Text = form.productNo;
        }

    }
}
